﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DMSClient
{
    public partial class frmClassification : Form
    {
        WSHttpBinding httpBinding = new WSHttpBinding();
        EndpointAddress address = new EndpointAddress("http://localhost:4872/DmsServices.svc");
        DMSServices.IDmsServices cliente;
        DataSet dsMaestro = new DataSet();
        DataTable dtP;
        DataTable dtH;
        string ambiente;


        public frmClassification()
        {
            InitializeComponent();
            ambiente = Properties.Settings.Default.CurrentEnv;
            httpBinding.MaxReceivedMessageSize = 2147483647;
            httpBinding.MaxBufferPoolSize = 2147483647;
            httpBinding.CloseTimeout = TimeSpan.MaxValue;
            httpBinding.ReceiveTimeout = TimeSpan.MaxValue;
            httpBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;

            cliente = new DMSServices.DmsServicesClient(httpBinding, address);
            this.Text = ambiente + " - Store Classification";

            uceCuontry.DisplayMember = "dsCountry";
            uceCuontry.ValueMember = "cdCountry";
            string distributors = cliente.getCountryList(ambiente);

            DataTable dt = new DataTable("dtCountryList");
            dt.Columns.Add("idCountry");
            dt.Columns.Add("cdCountry");
            dt.Columns.Add("dsCountry");

            DataSet ds = new DataSet("dsCountryList");
            ds.Tables.Add(dt);

            System.IO.StringReader xmlSR = new System.IO.StringReader(distributors);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
            uceCuontry.DataSource = dt;
            uceCuontry.SelectedIndex = 0;
            xmlSR.Close();


        }

        private void frmClassification_Load(object sender, EventArgs e)
        {

        }

        private void ubSearch_Click(object sender, EventArgs e)
        {
            cambio(false);
            bgwClass.RunWorkerAsync();
            
        }

        public DataTable filtroTabla(DataTable tab, string filtro)
        {
            DataRow[] rows;
            DataTable dtNew = tab.Clone();

            rows = tab.Select(filtro);

            foreach (DataRow row in rows)
            {
                dtNew.ImportRow(row);
            }
            return dtNew;
        }

        private void loader1_Load(object sender, EventArgs e)
        {

        }

        private void ubExcel_Click(object sender, EventArgs e)
        {

        }

        private void cambio(bool estado) {
                    ubExport.Enabled = estado;
                    ubSearch.Enabled = estado;
                    ugClass.Enabled = estado;
                    uceCuontry.Enabled = estado;
                    loader1.Visible = !estado;
        }

        private void bgwClass_DoWork(object sender, DoWorkEventArgs e)
        {
            string listarClass = cliente.getClassificationReport(uceCuontry.Value.ToString(),ambiente);
            DataTable dt = new DataTable("Classificationdt");
            dt.Columns.Add("idParentClassification");
            dt.Columns.Add("idClassification");
            dt.Columns.Add("cdClassification");
            dt.Columns.Add("nmClassification");
            DataSet ds = new DataSet("Classification");
            ds.Tables.Add(dt);

            System.IO.StringReader xmlSR = new System.IO.StringReader(listarClass);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
            xmlSR.Close();


            dtP = filtroTabla(dt, "idParentClassification = 0");
            dtP.Columns.Remove("idParentClassification");
            dtP.TableName = "padre";
            dtH = filtroTabla(dt, "idParentClassification <> 0");
            dtH.Columns.Remove("idClassification");
            dtH.TableName = "hijo";

            dsMaestro.Tables.Add(dtP);
            dsMaestro.Tables.Add(dtH);
            DataColumn dcPadre = new DataColumn();
            DataColumn dcHijo = new DataColumn();

            dcPadre = dsMaestro.Tables["padre"].Columns["idClassification"];
            dcHijo = dsMaestro.Tables["hijo"].Columns["idParentClassification"];

            DataRelation drRel = new DataRelation("Relacion", dcPadre, dcHijo);
            dsMaestro.Relations.Add(drRel);
        }

        private void bgwClass_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ugClass.DataSource = dsMaestro;
            try
            {
                ugClass.DisplayLayout.Bands[0].Columns["idClassification"].Hidden = true;
                ugClass.DisplayLayout.Bands[1].Columns["idParentClassification"].Hidden = true;
            }
            catch { }
            cambio(true);
        }

        private void ultraTabControl1_SelectedTabChanged(object sender, Infragistics.Win.UltraWinTabControl.SelectedTabChangedEventArgs e)
        {

        }

        private void ubExport_Click(object sender, EventArgs e)
        {
            if (ugClass.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Store Classification Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugClass, dialogo.FileName);
                if (MessageBox.Show(this, "Do you want to open the file?", "Store Classification Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }

        private void centrarLoader()
        {
            loader1.Left = (this.Width - loader1.Width) / 2;
            loader1.Top = ((this.Height - loader1.Height) / 2) - loader1.Height;
        }

        private void frmClassification_Shown(object sender, EventArgs e)
        {
            centrarLoader();
        }

        private void frmClassification_Resize(object sender, EventArgs e)
        {
            centrarLoader();
        }

    }
}
